<template>
  <div>
    <table-content
    :operaList="operaList"
    :tableList="tableList"
    :tableData="tableData"
    :res="res"
    @searchData="searchData"
    @getData="getData"
    @deleteData="deleteData"
    @addData="addData"
    @editData="editData" />
  </div>
</template>

<script>
import qs from 'qs'
import '@/mock/default.js'
import {
  addRole,
  updateRole,
  getRole,
  deleteRole,
  findAllRole
} from '@/api/power/role'
import { roleOperaList as operaList } from '@/assets/staticData/operaList.js'
import { roleTableList as tableList } from '@/assets/staticData/tableList.js'
import TableContent from '@/components/defaultTable/TableContent/index'
export default {
  name: '',
  components: {
    TableContent
  },
  props: {},
  data () {
    return {
      tableList: [],
      tableData: [],
      res: ''
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getData()
  },
  mounted () {},
  methods: {
    // addData (data) {
    //   this.$axios.get('http://127.0.0.1:7002/addRes')
    //     .then(res => {
    //       // console.log(res)
    //       this.res = res.data
    //     })
    // },
    // editData (_id, data) {
    //   this.$axios.get('http://127.0.0.1:7002/editRes')
    //     .then(res => {
    //       // console.log(res)
    //       this.res = res.data
    //     })
    // },
    // getData (data) {
    //   this.operaList = operaList
    //   this.tableList = tableList
    //   this.$axios.get('http://127.0.0.1:7002/tableData')
    //     .then(res => {
    //       // console.log(res)
    //       this.tableData = res.data.tableData
    //     })
    // },
    // deleteData (data) {
    //   this.$axios.get('http://127.0.0.1:7002/delRes')
    //     .then(res => {
    //       // console.log(res)
    //       this.res = res.data
    //     })
    // }
    searchData (data) {
      this.operaList = operaList
      this.tableList = tableList
      getRole({key: data})
        .then(res => {
          if (res.data.code === 1000) {
            if (res.data.data !== null) {
              this.tableData = res.data.data
            } else {
              this.tableData = []
              this.$message({
                type: 'warning',
                message: '角色数据为空!'
              })
            }
          }
        })
    },
    addData (data) {
      addRole(qs.stringify(data))
        .then(res => {
          this.getData(data)
          if (res.data.code === 1020) {
            console.log('已存在')
          } else {
            console.log('插入成功')
          }
          this.res = res
        })
        .catch(Error => {
          console.log('err:', Error)
        })
    },
    editData (_id, data) {
      updateRole(data._id, data)
        .then(res => {
          this.getData(data)
          this.res = res
        })
    },
    getData (data) {
      this.operaList = operaList
      this.tableList = tableList
      findAllRole()
        .then(res => {
          this.res = res
          this.tableData = res.data.data
        })
    },
    deleteData (data) {
      deleteRole(data._id, data)
        .then(res => {
          this.getData(data)
          this.res = res
        })
    }
  }
}
</script>

<style scoped>
</style>
